package cn.iocoder.yudao.module.course.controller.admin.coursematerials;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.course.controller.admin.coursematerials.vo.*;
import cn.iocoder.yudao.module.course.dal.dataobject.coursematerials.CourseMaterialsDO;
import cn.iocoder.yudao.module.course.service.coursematerials.CourseMaterialsService;

@Tag(name = "管理后台 - 课程素材")
@RestController
@RequestMapping("/course/materials")
@Validated
public class CourseMaterialsController {

    @Resource
    private CourseMaterialsService materialsService;

    @PostMapping("/create")
    @Operation(summary = "创建课程素材")
    @PreAuthorize("@ss.hasPermission('course:materials:create')")
    public CommonResult<Long> createMaterials(@Valid @RequestBody CourseMaterialsSaveReqVO createReqVO) {
        return success(materialsService.createMaterials(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程素材")
    @PreAuthorize("@ss.hasPermission('course:materials:update')")
    public CommonResult<Boolean> updateMaterials(@Valid @RequestBody CourseMaterialsSaveReqVO updateReqVO) {
        materialsService.updateMaterials(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程素材")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('course:materials:delete')")
    public CommonResult<Boolean> deleteMaterials(@RequestParam("id") Long id) {
        materialsService.deleteMaterials(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程素材")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('course:materials:query')")
    public CommonResult<CourseMaterialsRespVO> getMaterials(@RequestParam("id") Long id) {
        CourseMaterialsDO materials = materialsService.getMaterials(id);
        return success(BeanUtils.toBean(materials, CourseMaterialsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程素材分页")
    @PreAuthorize("@ss.hasPermission('course:materials:query')")
    public CommonResult<PageResult<CourseMaterialsRespVO>> getMaterialsPage(@Valid CourseMaterialsPageReqVO pageReqVO) {
        PageResult<CourseMaterialsDO> pageResult = materialsService.getMaterialsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseMaterialsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程素材 Excel")
    @PreAuthorize("@ss.hasPermission('course:materials:export')")
    @OperateLog(type = EXPORT)
    public void exportMaterialsExcel(@Valid CourseMaterialsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseMaterialsDO> list = materialsService.getMaterialsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程素材.xls", "数据", CourseMaterialsRespVO.class,
                        BeanUtils.toBean(list, CourseMaterialsRespVO.class));
    }

}